#include <iostream>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#include <vector>

#define SIZE 1
#define THREADCOUNT 1

class RingQueue
{
    public:
        RingQueue()
            :Vec_(SIZE)
        {
            CapaCity_ = SIZE;
            PosWrite_ = 0;
            PosRead_ = 0;

            //初始化策略是：信号量计数器的值和数组的空闲空间一样大
            sem_init(&ProSem_, 0, SIZE);
            //初始化的策略是：在初始化的时候，由于数组没有一个有效元素，所有初始化资源数为0，后边生产线程在唤醒消费线程的时候，会对消费者信号量当中的计数器进行加加操作，从而消费者线程可以获取到消费信号量，进而去访问数组
            sem_init(&ConSem_, 0, 0);

            //初始化的策略是，初始化资源数为1，即只有一个线程在同一时刻能够拥有该信号量
            sem_init(&Lock_, 0, 1);
        }

        ~RingQueue()
        {
            sem_destroy(&ProSem_);
            sem_destroy(&ConSem_);
            sem_destroy(&Lock_);
        }

        void Push(int& Data)
        {

            //如果将 sem_wait(&ProSem_); 和 sem_wait(&Lock_);交换位置是否可以？
            sem_wait(&ProSem_);

            sem_wait(&Lock_);
            Vec_[PosWrite_] = Data;
            PosWrite_ = (PosWrite_ + 1) % CapaCity_;
            sem_post(&Lock_);

            sem_post(&ConSem_);
        }

        void Pop(int* Data)
        {
            sem_wait(&ConSem_);

            sem_wait(&Lock_);
            *Data = Vec_[PosRead_];
            PosRead_ = (PosRead_ + 1) % CapaCity_;
            sem_post(&Lock_);

            sem_post(&ProSem_);
        }
    private:
        std::vector<int> Vec_;
        size_t CapaCity_;

        //读写位置
        int PosWrite_;
        int PosRead_;

        //同步
        sem_t ProSem_;
        sem_t ConSem_;

        //互斥
        sem_t Lock_;
};


void* ConsumeStart(void* arg)
{
    RingQueue* rq = (RingQueue*)arg;
    int Data;
    while(1)
    {
        rq->Pop(&Data);
        printf("ConsumeStart [%p][%d]\n", pthread_self(), Data);
    }
    return NULL;
}

void* ProductStart(void* arg)
{
    RingQueue* rq = (RingQueue*)arg;
    int i = 0;
    while(1)
    {
        rq->Push(i);
        printf("ProductStart [%p][%d]\n", pthread_self(), i);
        i++;
    }
    return NULL;
}

int main()
{
    RingQueue* rq = new RingQueue();

    pthread_t com_tid[THREADCOUNT], pro_tid[THREADCOUNT];

    int i = 0;
    for(; i < THREADCOUNT; i++)
    {
        int ret = pthread_create(&com_tid[i], NULL, ConsumeStart, (void*)rq);
        if(ret < 0)
        {
            printf("create thread failed\n");
            return 0;
        }

        ret = pthread_create(&pro_tid[i], NULL, ProductStart, (void*)rq);
        if(ret < 0)
        {
            printf("create thread failed\n");
            return 0;
        }
    }

    for(i = 0; i < THREADCOUNT; i++)
    {
        pthread_join(com_tid[i], NULL);
        pthread_join(pro_tid[i], NULL);
    }

    delete rq;
    rq = NULL;
    return 0;
}
